if(window.jQuery && window.jQuery.PPF && !window.$PPF.shitiedit) {
	(function($){
		$PPF.extend({
			shitiedit : {
				shitis:null,
				dls:null,
				stypes:null,
				did:0,
				sjid:0,
				init: function(d){
					d = d || this.did;
					this.buildAllShitiEtem(d);
				}
				,config: function(param){
					this.shitis = typeof param.shitis == "object" ? param.shitis : null;
					this.dls = typeof param.dls == "object" ? param.dls : null;
					this.stypes = typeof param.stypes == "object" ? param.stypes : null;
					this.did = param.did;
					this.sjid = param.sjid;
				}
				,getStType: function(t){ try{return this.stypes[t].tixing;}catch(e){return 'danxuan';} }
				,createShitiEtem: function(id){
					data = this.shitis[id];
					var content = 'loading';
					var title = data.title;
					switch(this.getStType(data.type)){
						case 'danxuan':
							content = PPF.html.radios({
								options: data.xx,
								checked: data.daan,
								name:'st_'+id,
								_key: "k",
								_val: "xx",
								abc: true,
								disabled: "disabled"
							});
							var ext = "分值："+ PPF.formatFen(data.fen);
							//if(data.kf>0) 
							ext += " 答错扣分：" + PPF.formatFen(data.kf);
							title = title + "（" + ext + "）";
							break;
						case 'duoxuan':
							content = PPF.html.checkboxes({
								options: data.xx,
								checked: data.daan,
								name:'st_'+id,
								noadd:true,
								_key: "k",
								_val: "xx",
								abc: true,
								disabled: "disabled"
							});
							var ext = "分值："+ PPF.formatFen(data.fen);
							//if(data.kf>0) 
							ext += " 答错扣分：" + PPF.formatFen(data.kf);
							//if(data.hfen>0) 
							ext += " 少选得分：" + PPF.formatFen(data.hfen);
							title = title + "（" + ext + "）";
							break;
						case 'xinli':
							var xx = {};
							for(var e in data.xx){
								var tmp = {k:data.xx[e].k,xx:data.xx[e].xx + "（选项分值：" + PPF.formatFen(data.xx[e].fen) + "）" };
								xx[e] = tmp;
							}
							content = PPF.html.radios({
								options: xx,
								checked: data.daan,
								name:'st_'+id,
								_key: "k",
								_val: "xx",
								abc: true,
								disabled: "disabled"
							});
							break;
						case 'jianda':
							content = data.daan;
							var ext = "分值："+ PPF.formatFen(data.fen);
							title = title + "（" + ext + "）";
							break;
					}
					return $("#shiti_tpl").content({
						title:title,
						content: content,
						jieda: data.jieda,
						id: id
					});
				}
				,rebuildShitiEtem: function(id){
					$("#_shitiItem_"+id).replaceWith(this.createShitiEtem(id));
				}
				,buildAllShitiEtem: function(d){
					d = d || this.did;
					this.did = d;
					var list = $("#shitiItemList").empty();
					var h = false;
					for(var e in this.shitis){
						if(this.shitis[e].did == d){
							list.append( this.createShitiEtem(e) );
							h = true;
						}
					}
					if(!h){
						list.append("<li>暂时没有试题</li>");
					}
				}
				,replaceShitiEtem: function(id1,id2){
					var o1 = $.extend(true,{},this.shitis[id1]);
					var o2 = $.extend(true,{},this.shitis[id2]);
					this.shitis[id1] = o2;
					this.shitis[id2] = o1;
					this.init();
				}
				,removeShitiEtem: function(id){delete this.shitis[id];$("#_shitiItem_" + id).remove();}
				,setShitiData: function(id,data){this.shitis[id] = data}
				,delShiti: function(id,t){
					if(t){
						var param = {ac: "del",xtid:id,sjid: this.sjid, did:this.did, inajax:1};
						$.post(__System.roots.exam + 'manage_shijuan/shiti',param,function(data){
							if( data._code == 0 ) {
								//replace tag
								PPF.shitiedit.removeShitiEtem(id);
								PPF.tips( {msg: PPF.jsonmsg(data,"删除成功") , timeout:1000} );
								//parent.shijuan update
								if(typeof parent.PPF.shijuan == "object"){
									parent.PPF.shijuan.dlnum[PPF.shitiedit.did] -= 1;
									parent.PPF.shijuan.updategu();
								}
							}else {
								PPF.tips( {msg: PPF.jsonmsg(data,"删除失败") , timeout:2000 ,type:1} );
							}
						},'json');
					}else{
						PPF.dialog( {title:"删除试卷段落" , msg:"确定删除？不可恢复！" , type:2 , callback: function(){return PPF.shitiedit.delShiti(id,true);} } );
					}
				}
				,moveup: function(id,up){
					var param = {ac: up ? "up" : "down",xtid:id,sjid: this.sjid, did:this.did, inajax:1};
					$.post(__System.roots.exam + 'manage_shijuan/shitimove',param,function(data){
							if( data._code == 0 ) {
								//replace tag
								PPF.shitiedit.replaceShitiEtem( data.id , data.rid );
								PPF.tips( {msg: PPF.jsonmsg(data,"移动成功") , timeout:1000} );
							}else {
								PPF.tips( {msg: PPF.jsonmsg(data,"移动失败") , timeout:2000 , type: 1} );
							}
						},'json');
				}
			}
		});
		
	})(jQuery);
}